package com.atguigu.gulimall.product.web;


import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping({"/", "/index.html"})
    public String reIndict(Model model) {
        //查找一级分类
        List<CategoryEntity> categoryEntityList = categoryService.getCategoryLevel1();
        model.addAttribute("categorys", categoryEntityList);
        return "index";
    }


    @ResponseBody
    @RequestMapping("/index/json/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> reMap = categoryService.getCatalogJson();
        return reMap;
    }


    @ResponseBody
    @RequestMapping("/hello")
    public String getSameple() {
        return "hello";
    }


    /**
     * 读写锁  读写锁都是成对出现的，读锁读，写锁写
     **/
    @ResponseBody
    @RequestMapping("/write")
    public String write() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = readWriteLock.writeLock();
        try {
            rLock.lock();
            s = UUID.randomUUID().toString();
            stringRedisTemplate.opsForValue().set("writeValue", s);
            Thread.sleep(3000);
        } catch (InterruptedException e) {

        } finally {
            rLock.unlock();
        }
        return s;
    }


    @ResponseBody
    @RequestMapping("/read")
    public String read() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = readWriteLock.readLock();
        try {
            rLock.lock();
            s = stringRedisTemplate.opsForValue().get("writeValue");
        } finally {
            rLock.unlock();
        }
        return s;
    }



    /**
     * Author : hmy
     * @date 2021/8/22 20:54
     * desc : 信号量   学校锁大门，只有等所有的班级全部走完之后，才能锁大门
     **/

    @ResponseBody
    @RequestMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(2);
        door.await();
        return "放假了，学校关门了";
    }


    @ResponseBody
    @RequestMapping("/gogo/{id}")
    public String gogo(@PathVariable("id") Long id) throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();
        return id+"班的人走了";
    }




















}
